/**
 * 
 */
package com.gr.staffpm.pages.schedule;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;

import com.gr.staffpm.pages.schedule.model.Event;

/**
 * @author Graham Rhodes 19 Mar 2011 22:30:11
 */
public class EventPanel extends Panel {

    private static final long serialVersionUID = 1L;
    private final RepeatingView events = new RepeatingView("events");
    private boolean hasChild = false;

    //private final Logger log = LoggerFactory.getLogger(getClass());

    public EventPanel(String id) {
        super(id);

        add(events);
    }

    public void add(final Event event, final WeekRow row, final int panelLength) {
        if (!event.isEmpty()) {
            //log.info("Event: " + event.getTitle());
            int tmp = event.getDaysSpannedTo(row.getWeekEnd());
            if (event.getStart().isBefore(row.getWeekStart()))
                tmp = event.getDaysSpannedFrom(row.getWeekStart(), row.getWeekEnd());
            final int daysSpanned = tmp;
            //log.info("Days spanned: " + daysSpanned);
            events.add(new WebMarkupContainer(RandomStringUtils.randomNumeric(10)) {

                private static final long serialVersionUID = 1L;

                @Override
                protected void onComponentTag(ComponentTag tag) {
                    //log.info("Event: " + event.getTitle() + " Panel Length: " + panelLength + " Days Spanned: " + daysSpanned);
                    int colspan = daysSpanned > 7 ? (7 - panelLength) : (daysSpanned <= 0 ? 1 : daysSpanned);
                    //log.info("Colspan: " + colspan);
                    if (colspan > 0)
                        tag.getAttributes().put("colspan", String.valueOf(colspan));
                    tag.getAttributes().put("class", "ext-cal-ev");
                };

            }.add(new Label("title", event.getTitle()) {

                private static final long serialVersionUID = 1L;

                @Override
                protected void onComponentTag(ComponentTag tag) {
                    String cls = "ext-color-" + event.getCalendar().getId() + (event.isAllDay() ? "-ad " : " ") + " ext-cal-evt ext-cal-evr ";
                    boolean spanLeft = event.startsBefore(row.getWeekStart());
                    boolean spanRight = event.endsOnOrAfter(row.getWeekEnd());
                    cls += (spanLeft ? (spanRight ? "ext-cal-ev-spanboth" : "ext-cal-ev-spanleft") : (spanRight ? "ext-cal-ev-spanright" : ""));
                    tag.getAttributes().put("class", cls);
                }

            }));
        } else {
            events.add(new WebMarkupContainer(RandomStringUtils.randomNumeric(10)) {

                private static final long serialVersionUID = 1L;

                @Override
                protected void onComponentTag(ComponentTag tag) {
                    tag.getAttributes().put("rowspan", "1");
                };

            }.add(new Label("title").setVisible(false)));
        }
        hasChild = true;
    }

    public void add(final Event event, final int colspan, final int rowspan, final WeekRow row, final int panelLength) {
        if (!event.isEmpty()) {
            //log.info("Event: " + event.getTitle());
            int tmp = event.getDaysSpannedTo(row.getWeekEnd());
            if (event.getStart().isBefore(row.getWeekStart()))
                tmp = event.getDaysSpannedFrom(row.getWeekStart(), row.getWeekEnd());
            final int daysSpanned = tmp;
            //log.info("Days spanned: " + daysSpanned);
            events.add(new WebMarkupContainer(RandomStringUtils.randomNumeric(10)) {

                private static final long serialVersionUID = 1L;

                @Override
                protected void onComponentTag(ComponentTag tag) {
                    //log.info("Event: " + event.getTitle() + " Panel Length: " + panelLength + " Days Spanned: " + daysSpanned);
                    int colspan = daysSpanned > 7 ? (7 - panelLength) : (daysSpanned <= 0 ? 1 : daysSpanned);
                    //log.info("Colspan: " + colspan);
                    if (colspan > 0)
                        tag.getAttributes().put("colspan", String.valueOf(colspan));
                    tag.getAttributes().put("class", "ext-cal-ev");
                };

            }.add(new Label("title", event.getTitle()) {

                private static final long serialVersionUID = 1L;

                @Override
                protected void onComponentTag(ComponentTag tag) {
                    String cls = "ext-color-" + event.getCalendar().getId() + (event.isAllDay() ? "-ad " : " ") + " ext-cal-evt ext-cal-evr ";
                    boolean spanLeft = event.startsBefore(row.getWeekStart());
                    boolean spanRight = event.endsOnOrAfter(row.getWeekEnd());
                    cls += (spanLeft ? (spanRight ? "ext-cal-ev-spanboth" : "ext-cal-ev-spanleft") : (spanRight ? "ext-cal-ev-spanright" : ""));
                    tag.getAttributes().put("class", cls);
                }

            }));
        } else {
            events.add(new WebMarkupContainer(RandomStringUtils.randomNumeric(10)) {

                private static final long serialVersionUID = 1L;

                @Override
                protected void onComponentTag(ComponentTag tag) {
                    if (rowspan > 0)
                        tag.getAttributes().put("rowspan", rowspan);
                    if (colspan > 0)
                        tag.getAttributes().put("colspan", colspan);
                };

            }.add(new Label("title").setVisible(false)));
        }
        hasChild = true;
    }

    public boolean hasChildren() {
        return hasChild;
    }

}
